package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.SpecialBean;
import com.attendance.www.hr.beans.SpecialPageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ISpecialService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 特殊考勤
 * 
 * @version 2017/10/9-2017/10/10
 * @author 徐伟  张鑫
 *
 */
@Controller
@RequestMapping("/special")
public class SpecialController {

	@Resource
	private ISpecialService specialService;

	/**
	 * 获取全部特殊情况信息
	 * 
	 * @param request
	 * @param specialBean
	 * @return
	 */
	@RequestMapping(value = "/getSpecialList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSpecialList(HttpServletRequest request, @RequestBody SpecialBean specialBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		
		if (specialBean.getPageCount()==null||specialBean.getPageNo()==null) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo=specialBean.getPageNo();
		int pageCount=specialBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		specialBean.setStartNum(startNum);
		SpecialPageBean specialPageBean=new SpecialPageBean();
		specialPageBean = specialService.getSpecialList(specialBean);
		return ResultMap.success(specialPageBean);
	}

	/**
	 * 获取允许在特殊情况日的员工IDList
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeIdListBySpecialId.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getEmployeeIdListBySpecialId(HttpServletRequest request) {
		Integer specialId = Integer.valueOf(request.getParameter("specialId"));
		if (specialId == null ) {
			throw new ResultFaultException("0_00001");
		}
		SpecialBean specialBean= specialService.getEmployeeIdListBySpecialId(specialId);
		return ResultMap.success(specialBean);
	}

	/**
	 * 新增特殊情况信息
	 * @param request
	 * @param specialBean
	 * @return
	 */
	@RequestMapping(value = "/saveSpecial.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveSpecial(HttpServletRequest request, @RequestBody SpecialBean specialBean) {
		if (specialBean.getSpecialName()== null
				|| specialBean.getSpecialDate() == null
				|| specialBean.getEmployeeIdList().size()<=0
				|| specialBean.getEmployeeIdList()==null
				|| (StringUtils.isBlank(specialBean.getStartTime())
						&& StringUtils.isBlank(specialBean.getEndTime()))) {
			throw new ResultFaultException("0_00001");
		}
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if(specialService.verifyCloseAccount(specialBean)) {
			throw new ResultFaultException("4_00003");
		}
		if(specialService.verifySaveSpecial(specialBean)){
			throw new ResultFaultException("3_00001");
		}
		specialBean.setEmployeeId(userEmployee.getEmployeeId());
		if (specialService.saveSpecial(specialBean)) {
			return ResultMap.success();
		} else {
			throw new ResultFaultException("0_00010");
		}
	}

	/**
	 * 修改特殊考勤数据
	 * @param request
	 * @param specialBean
	 * @return
	 */
	@RequestMapping(value = "/updateSpecial.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateSpecial(HttpServletRequest request, @RequestBody SpecialBean specialBean) {
		if (specialBean.getSpecialName()== null
				|| specialBean.getSpecialDate() == null
				|| specialBean.getEmployeeIdList().size()<=0
				|| specialBean.getEmployeeIdList()==null
				|| (StringUtils.isBlank(specialBean.getStartTime())
						&& StringUtils.isBlank(specialBean.getEndTime()))) {
			throw new ResultFaultException("0_00001");
		}
		if(specialService.verifyCloseAccount(specialBean)) {
			throw new ResultFaultException("4_00003");
		}
		if (specialService.updateSpecial(specialBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00010");
	}

	/**
	 * 删除特殊考勤数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteSpecial.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteSpecial(HttpServletRequest request, @RequestBody SpecialBean specialBean) {
		if (specialBean.getSpecialId() == null
			||specialBean.getSpecialDate() == null
			||specialBean.getEmployeeIdList().size()<=0
			||specialBean.getEmployeeIdList()==null) {
			throw new ResultFaultException("0_00001");
		}
		if (specialService.verifyUpdateSpecial(specialBean)) {
			throw new ResultFaultException("3_00003");
		}
		if (specialService.deleteSpecial(specialBean.getSpecialId())) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00001");
	}
}
